package sys_serv

import (
	"strconv"
	"strings"
	"ubt-rbac-service/internal/app/model"
	"ubt-rbac-service/internal/app/model/sys_mod"
	"ubt-rbac-service/internal/app/util"
)

type sysDict struct{}

var SysDictServ = sysDict{}

// Add 新增字典
func (s *sysDict) Add(req *sys_mod.SysDictSaveReq) *model.Result {
	sqlStr := "INSERT INTO sys_dict(dict_name, dict_code, quan_pin, jian_pin, kind, sort, remarks, states, add_time, opt_org) " +
		"VALUES ($1, $2, $3, $4, $5, $6, $7, 1, EXTRACT(EPOCH FROM NOW()), $8)"
	retCode, retMsg := util.CudData(sqlStr, req.DictName, req.DictCode, req.QuanPin, req.JianPin, req.Kind, req.Sort, req.Remarks, req.OptOrg)
	return &model.Result{RetCode: retCode, RetMsg: retMsg}
}

// Exists 判断字典编码是否存在
func (s *sysDict) Exists(req *sys_mod.SysDictExtVo) bool {
	sqlStr := "SELECT COUNT(1) AS nums FROM sys_dict WHERE states=1 AND kind=$1 AND opt_org=$2 AND (dict_name=$3 OR dict_code=$4)"
	param := make([]interface{}, 0)
	param = append(param, req.Kind)
	param = append(param, req.OptOrg)
	param = append(param, req.DictName)
	param = append(param, req.DictCode)
	if req.DictId != "" {
		sqlStr += " AND dict_id!=$5"
		param = append(param, req.DictId)
	}
	if dbMap, err := util.GetOne(sqlStr, param...); err == nil && len(dbMap) > 0 {
		if dbMap["nums"].(int64) > 0 {
			return true
		}
	}
	return false
}

// Update 修改字典
func (s *sysDict) Update(req *sys_mod.SysDictSaveReq) *model.Result {
	sqlStr := "UPDATE sys_dict " +
		"SET dict_name=$1, dict_code=$2, quan_pin=$3, jian_pin=$4, kind=$5, sort=$6, remarks=$7, " +
		"upd_time=EXTRACT(EPOCH FROM NOW()) WHERE dict_id=$8"
	retCode, retMsg := util.CudData(sqlStr, req.DictName, req.DictCode, req.QuanPin, req.JianPin, req.Kind, req.Sort,
		req.Remarks, req.DictId)
	return &model.Result{RetCode: retCode, RetMsg: retMsg}
}

// Deletes 处理字典
func (s *sysDict) Deletes(req *sys_mod.SysDictProReq) *model.Result {
	var (
		param []interface{}
		q     string
		n     = 0
	)
	idList := strings.Split(req.DictIds, ",")
	for _, curId := range idList {
		n++
		q += "$" + strconv.Itoa(n) + ","
		param = append(param, curId)
	}
	q = q[:len(q)-1]
	sqlStr := "UPDATE sys_dict SET states=2, upd_time=EXTRACT(EPOCH FROM NOW()) WHERE dict_id IN (" + q + ")"
	retCode, retMsg := util.CudData(sqlStr, param...)
	return &model.Result{RetCode: retCode, RetMsg: retMsg}
}

// One 获取字典详情
func (s *sysDict) One(dictId string) *model.Result {
	sqlStr := "SELECT dict_id, dict_name, dict_code, quan_pin, jian_pin, kind, sort, remarks, states, add_time, " +
		"upd_time FROM sys_dict WHERE dict_id=$1"
	if dbMap, err := util.GetOne(sqlStr, dictId); err == nil && len(dbMap) > 0 {
		return &model.Result{RetCode: 1001, RetMsg: "ok", RetData: dbMap}
	} else {
		return &model.Result{RetCode: 1000, RetMsg: "暂无内容"}
	}
}

// Count 获取字典数量
func (s *sysDict) Count(req *sys_mod.SysDictPageReq) *model.Result {
	sqlStr := "SELECT COUNT(1) AS nums FROM sys_dict WHERE states=1 AND kind=$1 AND opt_org=$2"
	param := make([]interface{}, 0)
	param = append(param, req.Kind)
	param = append(param, req.OptOrg)
	var n = 2
	if req.SchName != "" {
		sqlStr += " AND ("
		nameArr := strings.Split(req.SchName, " ")
		for _, name := range nameArr {
			sqlStr += "dict_name LIKE '%'||$" + strconv.Itoa(n+1) + "||'%' OR " +
				"quan_pin LIKE '%'||$" + strconv.Itoa(n+2) + "||'%' OR " +
				"jian_pin LIKE '%'||$" + strconv.Itoa(n+3) + "||'%' OR " +
				"dict_code LIKE '%'||$" + strconv.Itoa(n+4) + "||'%' OR "
			param = append(param, name)
			param = append(param, name)
			param = append(param, name)
			param = append(param, name)
			n += 4
		}
		sqlStr = sqlStr[:len(sqlStr)-4]
		sqlStr += ")"
	}
	if dbMap, err := util.GetOne(sqlStr, param...); err == nil && len(dbMap) > 0 {
		return &model.Result{RetCode: 1001, RetMsg: "ok", RetData: dbMap["nums"]}
	} else {
		return &model.Result{RetCode: 1000, RetMsg: "暂无内容"}
	}
}

// Page 获取字典分页列表
func (s *sysDict) Page(req *sys_mod.SysDictPageReq) *model.Result {
	sqlStr := "SELECT dict_id, dict_name, dict_code, kind, sort, add_time FROM sys_dict WHERE states=1 AND kind=$1 AND opt_org=$2"
	param := make([]interface{}, 0)
	param = append(param, req.Kind)
	param = append(param, req.OptOrg)
	var n = 2
	if req.SchName != "" {
		sqlStr += " AND ("
		nameArr := strings.Split(req.SchName, " ")
		for _, name := range nameArr {
			sqlStr += "dict_name LIKE '%'||$" + strconv.Itoa(n+1) + "||'%' OR " +
				"quan_pin LIKE '%'||$" + strconv.Itoa(n+2) + "||'%' OR " +
				"jian_pin LIKE '%'||$" + strconv.Itoa(n+3) + "||'%' OR " +
				"dict_code LIKE '%'||$" + strconv.Itoa(n+4) + "||'%' OR "
			param = append(param, name)
			param = append(param, name)
			param = append(param, name)
			param = append(param, name)
			n += 4
		}
		sqlStr = sqlStr[:len(sqlStr)-4]
		sqlStr += ")"
	}
	sqlStr += " ORDER BY sort LIMIT $" + strconv.Itoa(n+1) + " OFFSET $" + strconv.Itoa(n+2)
	param = append(param, req.Limit)
	param = append(param, req.Offset)
	dbMap, err := util.GetAll(sqlStr, param...)
	if err == nil && len(dbMap) > 0 {
		return &model.Result{RetCode: 1001, RetMsg: "ok", RetData: dbMap}
	} else {
		return &model.Result{RetCode: 1000, RetMsg: "暂无内容"}
	}
}

// Select 获取字典下拉列表
func (s *sysDict) Select(req *sys_mod.SysDictSelReq) *model.Result {
	sqlStr := "SELECT dict_id sel_id, dict_name sel_name, dict_code sel_code, false chked FROM sys_dict WHERE states=1 AND kind=$1 AND opt_org=$2"
	param := make([]interface{}, 0)
	param = append(param, req.Kind)
	param = append(param, req.OptOrg)
	var n = 2
	if req.SchName != "" {
		sqlStr += " AND ("
		nameArr := strings.Split(req.SchName, " ")
		for _, name := range nameArr {
			sqlStr += "dict_name LIKE '%'||$" + strconv.Itoa(n+1) + "||'%' OR " +
				"quan_pin LIKE '%'||$" + strconv.Itoa(n+2) + "||'%' OR " +
				"jian_pin LIKE '%'||$" + strconv.Itoa(n+3) + "||'%' OR " +
				"dict_code LIKE '%'||$" + strconv.Itoa(n+4) + "||'%' OR "
			param = append(param, name)
			param = append(param, name)
			param = append(param, name)
			param = append(param, name)
			n += 4
		}
		sqlStr = sqlStr[:len(sqlStr)-4]
		sqlStr += ")"
	}
	sqlStr += " ORDER BY sort"
	if dbMap, err := util.GetAll(sqlStr, param...); err == nil && len(dbMap) > 0 {
		return &model.Result{RetCode: 1001, RetMsg: "ok", RetData: dbMap}
	} else {
		return &model.Result{RetCode: 1000, RetMsg: "暂无内容"}
	}
}

// Sort 获取数量，用于排序
func (s *sysDict) Sort(req *sys_mod.SysDictSortReq) *model.Result {
	sqlStr := "SELECT COUNT(1)+1 AS nums FROM sys_dict WHERE states=1 AND kind=$1 AND opt_org=$2"
	param := make([]interface{}, 0)
	param = append(param, req.Kind)
	param = append(param, req.OptOrg)
	if dbMap, err := util.GetOne(sqlStr, param...); err == nil && len(dbMap) > 0 {
		return &model.Result{RetCode: 1001, RetMsg: "ok", RetData: dbMap["nums"]}
	} else {
		return &model.Result{RetCode: 1000, RetMsg: "暂无内容"}
	}
}
